package com.fuchuang.seckillsystem.controller;

import com.fuchuang.seckillsystem.dto.GoodsQuery;
import com.fuchuang.seckillsystem.entity.Goods;
import com.fuchuang.seckillsystem.service.IGoodService;
import com.fuchuang.seckillsystem.vo.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("goods")
public class GoodsController {
    
    @Autowired
    IGoodService goodsService;
    
    @PostMapping("add")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_PRODUCT')")
    public RespBean addGoods(@RequestBody Goods goods) {
        goodsService.addGoods(goods);
        return RespBean.success();
    }
    
    @GetMapping("delete")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_PRODUCT')")
    public RespBean deleteGoods(@RequestParam("goodsId") Integer goodsId) {
        goodsService.deleteGoodsById(goodsId);
        return RespBean.success();
    }
    
    @PostMapping("update")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_PRODUCT')")
    public RespBean updateGoods(@RequestBody Goods goods) {
        goodsService.updateGoods(goods);
        return RespBean.success();
    }
    
    @GetMapping("selectOne")
    public RespBean selectById(@RequestParam("goodsId") Integer goodsId) {
        Goods goods = goodsService.selectGoodsById(goodsId);
        return RespBean.success(goods);
    }
    
    @PostMapping("selectAll")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_FORM','ROLE_PRODUCT')")
    public RespBean selectAllGoods(@RequestBody GoodsQuery goodsQuery) {
        Map<String, Object> map = goodsService.selectAllGoods(goodsQuery);
        return RespBean.success(map);
    }
    
}
